﻿<Window x:Class="DumpView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DumpHeapAnalyzer"
    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    Title="DumpView" >
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="File">
                <MenuItem Name="MenuPrint" 
                          Header="Print" 
                          Command="ApplicationCommands.Print"
                          />
            </MenuItem>
        </Menu>
        
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        
        <ListBox ItemsSource="{Binding Path=Snapshots}" 
                 Grid.Row="0"
                 HorizontalAlignment="Stretch" 
                 HorizontalContentAlignment="Stretch">
            <ListBox.Resources>
                <!-- override the highlight brush key so we don't have the default border when listbox item is clicked-->
                <SolidColorBrush 
							            x:Key="{x:Static SystemColors.HighlightBrushKey}"  
							            Color="Transparent" />
                <SolidColorBrush 
							            x:Key="{x:Static SystemColors.HighlightTextBrushKey}"  
							            Color="Black" />
            </ListBox.Resources>
            <ListBox.ItemTemplate>
                <DataTemplate DataType="{x:Type local:HeapSnapshot}">

                    <DockPanel Margin="0,0,0,3">
                        <StackPanel Orientation="Horizontal" DockPanel.Dock="Top"
                                            Background="AliceBlue" 
                                        >
                            <TextBlock Text="Dump Files/Source: "
                                            FontWeight="Bold" 
                                           Height="20" />
                            <TextBlock Text="{Binding Path=DumpFile.FileName}" 
                                            FontWeight="Bold"  />
                            
                            <Label Content="Created:" Margin="20,0,0,0" />
                            <TextBlock Text="{Binding Path=DumpFile.CreatedDate}" />
                            
                            <Label Content="Size (mb):" Margin="20,0,0,0" />
                            <TextBlock Text="{Binding Path=DumpFile.SizeInMB, Mode=OneWay}" />
                            
                        </StackPanel>

                        <ListBox ItemsSource="{Binding Path=Memory.Heaps}"
                                     BorderBrush="Transparent"
                                     BorderThickness="0"
                                     Padding="0,0,20,0"
                                     HorizontalAlignment="Stretch"
                                     HorizontalContentAlignment="Stretch">
                            <ListBox.ItemTemplate>
                                <DataTemplate DataType="{x:Type local:HeapInfo}">
                                    <DockPanel >
                                        <StackPanel DockPanel.Dock="Left" 
                                                    Background="AliceBlue"
                                                    Orientation="Vertical" 
                                                    Width="60">
                                            <TextBlock  TextDecorations="Underline" FontWeight="Bold">
                                                <Run Text="Heap:" />
                                                <Run Text="{Binding Path=Id}" />
                                            </TextBlock>
                                            <TextBlock>
                                                <Run Text="Total Size:" />
                                                <LineBreak />
                                                <Run Text="{Binding Path=SizeInMB, Mode=OneWay}" />
                                            </TextBlock>
                                        </StackPanel>
                                        <Grid HorizontalAlignment="Stretch">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition />
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition />
                                                <RowDefinition />
                                                <RowDefinition />
                                                <RowDefinition />
                                            </Grid.RowDefinitions>
                                            <TextBlock Grid.Row="0" Grid.Column="0"
                                                    Text="" />
                                            <TextBlock Grid.Row="0" Grid.Column="1"
                                                       HorizontalAlignment="Stretch"
                                                       TextAlignment="Center"
                                                       FontWeight="Bold"
                                                    Text="Gen0" />
                                            <TextBlock Grid.Row="0" Grid.Column="2"
                                                       HorizontalAlignment="Stretch"
                                                       TextAlignment="Center"
                                                       FontWeight="Bold"
                                                    Text="Gen1" />
                                            <TextBlock Grid.Row="0" Grid.Column="3"
                                                       HorizontalAlignment="Stretch"
                                                       TextAlignment="Center"
                                                       FontWeight="Bold"
                                                    Text="Gen2" />
                                            <TextBlock Grid.Row="0" Grid.Column="4"
                                                       HorizontalAlignment="Stretch"
                                                       TextAlignment="Center"
                                                       FontWeight="Bold"
                                                    Text="LOH" />

                                            <TextBlock Grid.Row="1" Grid.Column="0"
                                                        Text="Size (mb)" />
                                            <TextBlock Grid.Row="1" Grid.Column="1"
                                                       TextAlignment="Center"
                                                        Text="{Binding Path=Gen0.SizeInMB}" />
                                            <TextBlock Grid.Row="1" Grid.Column="2"
                                                       TextAlignment="Center"
                                                        Text="{Binding Path=Gen1.SizeInMB}" />
                                            <TextBlock Grid.Row="1" Grid.Column="3"
                                                       TextAlignment="Center"
                                                        Text="{Binding Path=Gen2.SizeInMB}" />
                                            <TextBlock Grid.Row="1" Grid.Column="4"
                                                       TextAlignment="Center"
                                                        Text="{Binding Path=LOHeap.SizeInMB}" />

                                            <TextBlock Grid.Row="2" Grid.Column="0"
                                                        Text="Free" />
                                            <TextBlock Grid.Row="2" Grid.Column="1"
                                                       TextAlignment="Center"
                                                        Text="{Binding Path=Gen0.FreeSpace}" />
                                            <TextBlock Grid.Row="2" Grid.Column="2"
                                                       TextAlignment="Center"
                                                        Text="{Binding Path=Gen1.FreeSpace}" />
                                            <TextBlock Grid.Row="2" Grid.Column="3"
                                                       TextAlignment="Center"
                                                        Text="{Binding Path=Gen2.FreeSpace}" />
                                            <TextBlock Grid.Row="2" Grid.Column="4"
                                                       TextAlignment="Center"
                                                        Text="{Binding Path=LOHeap.FreeSpace}" />

                                            <TextBlock Grid.Row="3" Grid.Column="0"  
                                                       Text="Pct" />
                                            <TextBlock Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3"
                                                       HorizontalAlignment="Stretch"
                                                       TextAlignment="Center">
                                                  <Run Text="{Binding Path=SOHPercent}" />
                                                  <Run Text="%" />
                                            </TextBlock>
                                            <TextBlock Grid.Row="3" Grid.Column="4" 
                                                       TextAlignment="Center">
                                                  <Run Text="{Binding Path=LOHPercent}" />
                                                  <Run Text="%" />
                                            </TextBlock>

                                        </Grid>
                                    </DockPanel>

                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </DockPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        
        <chartingToolkit:Chart Name="DumpBarChart"
                               Grid.Row="1" 
                               Title="Dump Memory"
                               LegendTitle="Dump">
            <chartingToolkit:Chart.Series>
                <chartingToolkit:ColumnSeries Title="Process" 
                                              ItemsSource="{Binding Path=Snapshots}"  
                                           IndependentValueBinding="{Binding Path=Name}"
                                           DependentValueBinding="{Binding Path=DumpFile.SizeInMB}" />
                <chartingToolkit:ColumnSeries Title="Gen0" 
                                              ItemsSource="{Binding Path=Snapshots}"  
                                           IndependentValueBinding="{Binding Path=Name}"
                                           DependentValueBinding="{Binding Path=Memory.Heaps[0].Gen0.SizeInMB}" />
                <chartingToolkit:ColumnSeries Title="Gen1" 
                                              ItemsSource="{Binding Path=Snapshots}"  
                                           IndependentValueBinding="{Binding Path=Name}"
                                           DependentValueBinding="{Binding Path=Memory.Heaps[0].Gen1.SizeInMB}" />
                <chartingToolkit:ColumnSeries Title="Gen2" 
                                              ItemsSource="{Binding Path=Snapshots}"  
                                           IndependentValueBinding="{Binding Path=Name}"
                                           DependentValueBinding="{Binding Path=Memory.Heaps[0].Gen2.SizeInMB}" />
                <chartingToolkit:ColumnSeries Title="LOH" 
                                              ItemsSource="{Binding Path=Snapshots}"  
                                           IndependentValueBinding="{Binding Path=Name}"
                                           DependentValueBinding="{Binding Path=Memory.Heaps[0].LOHeap.SizeInMB}" />
            </chartingToolkit:Chart.Series>
        </chartingToolkit:Chart>
    </Grid>
    </DockPanel>
</Window>
